﻿<cfscript>
/** 
* DataBase Frameworks - Sequence
* 
* @hint "DataBase Frameworks - Sequence - CETEntry" 
*/ 

component 
	displayname="public.service.db.senate.sequence.CETEntry" 
	output=false 
	accessors=true 
	{

	property type="string" name="termID" hint="学期";
	property type="string" name="studentID" hint="学号";
	
	/**
	* @hint "初始化对象"
	*/
	public function init() output=false {
		return this;
	}
	
	/**
	* @hint "自定义主键规则"
	*/
	public string function getID() output=false {
		
		var nextID = getTermID() & "," & getStudentID() & "," & numberFormat(1, "0_");

		var sql = "	SELECT
						ce.eid
					FROM
						t_cet_entry ce
					WHERE
						ce.term_id = :termId 
						AND
						ce.stu_id = :studentId 
					ORDER BY
						ce.eid DESC";
		
		var queryObj = new Query(datasource = application.dnsMaster);
		
		queryObj.addParam(name="termId", value=getTermId(), cfsqltype="cf_sql_char");
		queryObj.addParam(name="studentId", value=getStudentId(), cfsqltype="cf_sql_varchar");
		
		var rs_check = queryObj.execute(sql=sql).getResult();
		
		/* 
			主键生成规则 VARCHAR(30)
			
			3位学期代码, 20位学号, 2位流水号
			
		*/
		if (rs_check.recordCount) {
			/* 创建一个追加id */
			nextID = getTermID() & "," & getStudentID() & "," & numberFormat(right(rs_check['eid'][1], 2) + 1, "0_");
		}
		
		return nextID;
	}

}

</cfscript>